package com.baohua.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSON;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baohua.exception.BaohuaSystemException;
import com.baohua.model.InStore;
import com.baohua.model.Inventory;
import com.baohua.model.Staff;
import com.baohua.service.InStoreService;
import com.baohua.service.InventoryService;
import com.baohua.service.StaffService;
/**
 * 出库入库
 * @author czp
 *
 */
@Controller
@RequestMapping("/instore")
public class InstoreController {

	/**
	 * 返回采购进货主页面
	 * @return
	 */
	@RequestMapping("/index0")
	public String index0() {
		return "instore";
	}
	
	/**
	 * 返回领料出库主页面
	 * @return
	 */
	@RequestMapping("/index1")
	public String index1() {
		return "instore1";
	}
	
	/**
	 * 进入增加入库页面
	 * @return
	 */
	@RequestMapping("/addinstoreIndex")
	public String addInstoreIndex() {
		return "addinstore";
	}
	
	/**
	 * 进入出库操作页面
	 * @return
	 */
	@RequestMapping("/lessinstoreIndex")
	public String lessInstoreIndex() {
		return "lessinstore";
	}
	
	/**
	 * 查询出库(0)/入库(1)的数据
	 * @param instore 出入库对象
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public @ResponseBody Map queryByInstore(InStore instore, HttpServletRequest request) {
		Map map = new HashMap();
		try {
			//查询条件的前置日期
			String pre_warehousingDate = request.getParameter("pre_warehousingDate");
			//查询条件的后置日期
			String af_warehousingDate = request.getParameter("af_warehousingDate");
			//查询条件的前置数量
			String pre_warehousingAmount = request.getParameter("pre_warehousingAmount");
			//查询条件的后置数量
			String af_warehousingAmount = request.getParameter("af_warehousingAmount");
			//出库、入库, 出库为1, 入库为0
			String status = request.getParameter("status");
			//经办人的员工编号
			String staffNO = request.getParameter("staffNO");
			
			int page = Integer.parseInt(request.getParameter("page"));
			int pagesize = Integer.parseInt(request.getParameter("pagesize"));
			
			map = instoreService.queryInStore(instore, pre_warehousingDate, af_warehousingDate, 
					pre_warehousingAmount, af_warehousingAmount, status, staffNO, page, pagesize);
		} catch (ParseException e) {
			logger.info(e.getMessage());
		}
		return map;
	}
	
	
	/**
	 * 增加入库/出库
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/addInstore", method=RequestMethod.POST)
	public @ResponseBody String addInstore(HttpServletRequest request) {
		//获取入库(0)/出库(1)状态
		String status = request.getParameter("status");
		//获取单号
		String inStoreNO = request.getParameter("inStoreNO");
		//获取入库/出库时间
		String warehousingDate = request.getParameter("warehousingDate");
		//获取员工编号
		String staffNO = request.getParameter("staffNO");
		//获取备注
		String memo = request.getParameter("memo");
		//获取入库/出库明细ID(前台以 1, 2, 3的字符串传入)
		String arr_no = request.getParameter("arr_no");
		//获取入库/出库明细数量(前台以 1, 2, 3的字符串传入)
		String arr_amount = request.getParameter("arr_amount");
		try{
	    
		//判断传过来的值是否为空, 为空时, 返回错误信息
		if(!StringUtils.isBlank(arr_no)){
			instoreService.addInStore(status, inStoreNO, warehousingDate, staffNO, memo, arr_no, arr_amount);
		}else {
			return "{message:'error1'}";
		}
		} catch (ParseException e) {
			logger.info(e.getMessage());
			return "{message:'error'}";
		} catch (BaohuaSystemException e) {
			logger.info(e.getMessage());
			return "{message:'systemerror'}";
		} 
		return "{message:'success'}";
	}
	
	/**
	 * 采购入货的页面上增加新的物料信息
	 * @param inventory
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public String addInstore(Inventory inventory, Model model) {
		 try{
	    	 inventoryService.addRawMaterial(inventory);
		  } catch (BaohuaSystemException e) {
		 		logger.info(e.getMessage());
		 		model.addAttribute("errorMessage", e.getMessage());
		 		return "forward:/system/error";
		  } 
		return "redirect:/instore/addinstoreIndex";
	}
	
	@RequestMapping("export")
	public String export(HttpServletRequest request, HttpServletResponse response) {
		List<InStore> list = new ArrayList<InStore>(); 
		String param = request.getParameter("param");
		String status = request.getParameter("status");
		try {
			if(!StringUtils.isBlank(param)) {
			  String[] instoreIds = param.split(",");
			  list = instoreService.queryByInstoreIds(instoreIds);
			}
			
			Date date = new Date();
			String fileName = "";
			String sheetName = "";
			if(StringUtils.equals("0", status)){
				fileName = "入库列表";
				sheetName = "入库列表";
			}else if(StringUtils.equals("1", status)) {
				fileName = "入库列表";
				sheetName = "入库列表";
			}
			instoreService.exportData(fileName + date.getTime() + ".xls", 
					list, sheetName, request, response);
		} catch (IllegalArgumentException e) {
			logger.info(e.getMessage());
		} catch (IOException e) {
			logger.info(e.getMessage());
		} catch (IllegalAccessException e) {
			logger.info(e.getMessage());
		}
		return null;
	}
	
	private InventoryService inventoryService;
	private InStoreService instoreService;
	private StaffService staffService;
	private static final Logger logger = Logger.getLogger(InstoreController.class.getName());
	
	
	public StaffService getStaffService() {
		return staffService;
	}

	@Resource(name="staffService")
	public void setStaffService(StaffService staffService) {
		this.staffService = staffService;
	}
	
	
	public InStoreService getInstoreService() {
		return instoreService;
	}

	@Resource(name="instoreService")
	public void setInstoreService(InStoreService instoreService) {
		this.instoreService = instoreService;
	}

	public InventoryService getInventoryService() {
		return inventoryService;
	}

	@Resource(name="inventoryService")
	public void setInventoryService(InventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}
	
}
